package com.easychat.websocket.netty;

import io.netty.channel.Channel;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 心跳检测处理心跳的类
 * @author 10 2024/10/18
 */
@Slf4j
public class HandlerHeartBeat extends ChannelDuplexHandler {
     private static final Logger logger = LoggerFactory.getLogger(HandlerHeartBeat.class);

     /**
      * 处理心跳
      * @param ctx
      * @param evt
      * @throws Exception
      */
     @Override
     public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
          if(evt instanceof IdleStateEvent){
               IdleStateEvent e = (IdleStateEvent) evt;
               if(e.state() == IdleState.READER_IDLE){
                    Channel channel = ctx.channel();
                    Attribute<String> attribute = channel.attr(AttributeKey.valueOf(channel.id().toString()));
                    String userId = attribute.get();
                    logger.info("用户{}心跳超时",userId);
                    ctx.close();
               }else if(e.state() == IdleState.ALL_IDLE){
                    ctx.writeAndFlush("heart");
               }
          }
          super.userEventTriggered(ctx, evt);
     }
}
